         # 用于读写userInfo.txt文件的类
class UserInfo:
    def read_user_info():
        """
        读取userInfo.txt文件内容
        :return: 文件内容
        """
        try:
            file_path = "./infos/userInfo.txt"
            # 读取完成后的name与birthday组合成一个数组返回
            with open(file_path, 'r', encoding='utf-8') as file:
                # 读取文件内容
                file_content = file.read()
                # 按行分割内容
                lines = file_content.split('\n')
                # 提取姓名和生日信息
                name = lines[0].split('：')[1] if len(lines) > 0 else None
                birthday = lines[1].split('：')[1] if len(lines) > 1 else None
                # 返回姓名和生日信息
                return [name, birthday]
        except FileNotFoundError:
            print("文件未找到")
            return None
        except Exception as e:
            print(f"读取文件时出错：{e}")
            return None
    def write_user_info(name,birthday):
        """
        写入userInfo.txt文件内容
        :param content: 要写入的内容
        """
        try:
            # 判断name,birthday是否为空，如果为空则不写入文件
            if not name or not birthday:
                print("姓名或生日为空，不写入文件")
                return
            file_path = "./infos/userInfo.txt"
            content = f"姓名：{name}\n生日：{birthday}\n"
            with open(file_path, 'w', encoding='utf-8') as file:
                file.write(content)
            print("写入成功")
        except Exception as e:
            print(f"写入文件时出错：{e}")

        # 读写历史记录的方法
    def read_history():
        """读取最近50条历史记录"""
        try:
            with open("./infos/listHistory.txt", "r", encoding="utf-8") as f:
                return [line.strip() for line in f.readlines()]  # 修正1：去除换行符
        except FileNotFoundError:
            return []
        except Exception as e:
            print(f"读取历史记录失败: {str(e)}")
            return []
    def write_history_info(log_entry):
        """写入历史记录"""
        try:
            with open("./infos/listHistory.txt", "a", encoding="utf-8") as f:
                f.write("\n"+log_entry)  # 修正2：正确换行写入
        except Exception as e:
            print(f"历史记录写入失败: {str(e)}")
# 使用示例
if __name__ == '__main__':
    # 创建UserInfo类的实例，传入文件路径
    # 读取文件内容
    read_result = UserInfo.read_user_info()
    if read_result:
        print(f"姓名：{read_result[0]}, 生日：{read_result[1]}")
    # 写入文件内容
    UserInfo.write_user_info('李四1', '2290-01-01')
    